import { Component } from '@angular/core';
import {IonicPage, ModalController, NavController, NavParams} from 'ionic-angular';
import {ExchangeRoomConfirmPage} from "../exchange-room-confirm/exchange-room-confirm";
import {RepealRoomConfirmPage} from "../repeal-room-confirm/repeal-room-confirm";
import {ContractApiService, ContractMsg, Rentbill} from "../../api/contract-api.service";
import {ROUTER} from "../../service/router.service";
import {LocalStorage} from "../../utils/localstorage.utils";
import {CheckStandModel} from "../../model/checkstand.model";
import {USER} from "../../service/user.service";


@IonicPage()
@Component({
  selector: 'page-renting-detail',
  templateUrl: 'renting-detail.html',
})
export class RentingDetailPage {
  Rentbills:Array<Rentbill>=[];

  data:any={};

  private id: any;
  contractmsg:ContractMsg;


  constructor(public navCtrl: NavController,
              public navParams: NavParams,
              private modalCtrl: ModalController,
              private contractApiService: ContractApiService,
              private ls:LocalStorage,
              private USER:USER,
              private ROUTER: ROUTER) {
    this.id = navParams.get('id');
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad RentingDetailPage');
    this.contractApiService.getContractMsg(this.id,v => {
      this.contractmsg = v;
    })
    this.ls.setObject('contractId',this.id);
    this.contractApiService.getRentbill({
      contractId: this.id,
      type: 1
    }, v => {
      this.Rentbills = v.content
      console.log(this.Rentbills);
    })
  }

  exchange(){
    if(this.contractmsg.status==8){
      let profileModal = this.modalCtrl.create(ExchangeRoomConfirmPage, {ContractId:this.id}, {showBackdrop: true});
      profileModal.present();
    }else {
      this.USER.toast('合同状态异常，暂时不能换房');
    }

  }

  repeal(){
    if(this.contractmsg.status==8){
      let profileModal = this.modalCtrl.create(RepealRoomConfirmPage,this.data,{showBackdrop: true});
      profileModal.present();
    }else {
      this.USER.toast('合同状态异常，暂时不能退房');
    }

  }

  getRecentBill():Rentbill{
    for(let v of this.Rentbills){
      if(v.status == 1){
        console.log(v)
        return v;
      }
    }
    return null
  }
  gotoContract(pdf:any){
    window.open(pdf);
  }
  gotoPayRent() {
    let rentBill = this.getRecentBill();
    console.log(rentBill);
    if (rentBill ==null){
      this.USER.toast("您没有房租需要缴纳");
    }else {
      let checkstand = new CheckStandModel(rentBill.uuid, rentBill.content, rentBill.money, rentBill.contractId,ROUTER.RENTDETAIL)
      this.ls.setObject('checkstand', checkstand);
      this.ROUTER.go(ROUTER.CHECK);
    }
  }
  ionViewDidEnter(){
    document.title="交租记录";
  }

}
